/*
 * XmlLessonLoader.java
 *
 * Created on 5. Dezember 2006, 21:02
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.lesson;

import com.sun.org.apache.xerces.internal.parsers.SAXParser;
import java.io.FileInputStream;
import kanjitori.abstraction.Factory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Pirx
 */
public class XmlLessonLoader implements Factory<Lesson> {
    
    public Lesson get(String id) {
        try {
            Lesson lesson = new Lesson();
            XMLReader xmlReader = new SAXParser();
            xmlReader.setContentHandler(new LessonHandler(lesson));
            InputSource is = new InputSource(new FileInputStream(id));
            xmlReader.parse(is);
            return lesson;
        } catch(Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    
    private static class LessonHandler extends DefaultHandler {
        
        private Lesson lesson;
        private Section section;
        
        LessonHandler(Lesson lesson) {
            this.lesson = lesson;
        }
        
        @Override
        @SuppressWarnings( "unchecked" )
        public void startElement(String uri, String localName, String qName, Attributes attributes){
            if ("section".equals(qName)) {
                String name = attributes.getValue("name");
                section = new Section(name);
            } else if("entry".equals(qName)) {
                String key = attributes.getValue("key");
                String values = attributes.getValue("value");
                String ordered = attributes.getValue("ordered");
                section.addEntry(new Entry(key, values.split("\\|"), ! "false".equalsIgnoreCase(ordered)));
            } else if ("bot".equals(qName)) {
                lesson.setBotType(attributes.getValue("type"));
            } else if ("param".equals(qName)) {
                lesson.getBotParams().put(attributes.getValue("key"), 
                                          attributes.getValue("value"));
            } else if ("lesson".equals(qName)) {
                lesson.setName(attributes.getValue("name",""));
                lesson.setDescription(attributes.getValue("description",""));
                lesson.setAuthor(attributes.getValue("author",""));
            }
        }
        
        @Override
        public void endElement(String uri, String localName, String qName) {
            if ("section".equals(qName)) {
                lesson.addSection(section);
                section = null;
            }
        }
    }

}
